package com.yj.rest;

import com.mysql.cj.xdevapi.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class EquipmentLocationService {

    @Autowired
    private EquipmentLocationMapper mapper;


    public void getTree() {

        // 1. 先查出所有数据
        List<EquipmentLocation> equipmentLocationList = mapper.getList();
        // 2. 根据parentId聚合数据
        Map<String, List<EquipmentLocation>> parentMap = equipmentLocationList.stream().collect(Collectors.groupingBy(EquipmentLocation::getParentId));
        for (String parentId : parentMap.keySet()) {
            // 3. 将子节点塞到父节点数据中
            List<EquipmentLocation> parentList = parentMap.get(parentId);
            for (EquipmentLocation parentBean : parentList) {
                parentBean.setChildren(parentMap.get(parentBean.getId()));
            }
        }
        List<EquipmentLocation> result = parentMap.get("0");
        return;
    }
}
